//
//  NSString+date.m
//  JMDome_1
//
//  Created by ios on 15/6/17.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "NSString+date.h"

@implementation NSString (date)


+ (NSDateFormatter *)dataFormatter{
    static NSDateFormatter *dateFormatter = nil;
    if(!dateFormatter){
        dateFormatter = [[NSDateFormatter alloc]init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        [dateFormatter setDateStyle:NSDateFormatterNoStyle];
        }
    return dateFormatter;
}
+ (NSString *)dateWithSting:(NSDate *)fromeDate{
    
    if(fromeDate){
        return  [[self dataFormatter] stringFromDate:fromeDate];
    }else{
        return  [[self dataFormatter] stringFromDate:[NSDate date]];
    }
}

+ (NSString *) dateInterval:(NSDate *)oldDate nowDate:(NSDate *)nowDate{

    NSCalendar *calendar = [NSCalendar currentCalendar];//日历
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:oldDate toDate:[NSDate date] options:0];
    int year = (int)[components year];
    int month = (int)[components month];
    int day = (int)[components day];
    int h = (int) [components hour];
    int m = (int) [components minute];
    //三天以内更改显示格式
    NSString *title = nil;
    if (year == 0 && month == 0 && day < 3) {
        if (day == 0) {
            if(h == 0){
                title = [NSString stringWithFormat:@"%d分钟前",m];
                return title;
            }else{
                title = [NSString stringWithFormat:@"%d小时%d分钟前",h,m];
                return title;
            }
            title = NSLocalizedString(@"今天",nil);
        } else if (day == 1) {
            title = NSLocalizedString(@"昨天",nil);
        } else if (day == 2) {
            title = NSLocalizedString(@"前天",nil);
        }else{
            title = [NSString stringWithFormat:@"%d年%d月",year,month];
        }
    }
    return title;
}

@end

@implementation NSString (font)

- (CGSize)sizeWithFont:(UIFont *)font andMaxSize:(CGSize)maxSize{
    NSDictionary *arrts = @{NSFontAttributeName:font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:arrts context:nil].size;
}

+ (CGSize)sizeWithText:(NSString *)text andFont:(UIFont *)font andMaxSize:(CGSize)maxSize{
    return [text sizeWithFont:font andMaxSize:maxSize];
}


@end

@implementation NSString (JMMD5)

-(NSString *)JMMD5String{
    return [[self MD5Digest]substringWithRange:NSMakeRange(8, 16)];
}

@end

